package com.netease.nim.demo.session.viewholder;

import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.netease.nim.demo.common.media.watch.image.WatchMessagePictureActivity;
import com.netease.nim.demo.common.ui.imageview.MsgThumbImageView;
import com.netease.nim.demo.common.util.media.ImageUtil;
import com.netease.nim.demo.common.util.media.ImageUtil.ImageSize;
import com.netease.nim.demo.common.util.string.StringUtil;
import com.netease.nim.demo.session.model.MsgListItem;
import com.netease.nimlib.sdk.msg.attachment.ImageAttachment;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.shwread.android.qysw10000227.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.netease.nim.demo.base.YXBaseActivity.items;


public class MsgViewHolderPictureRight
        extends
        MsgViewHolder
{
	public static final int					MIN_SDK_ENABLE_LAYER_TYPE_HARDWARE = Build.VERSION_CODES.ICE_CREAM_SANDWICH;
	public static List<MsgListItem>			messageItems2					   = new ArrayList<>();
	public static HashMap<Integer, Integer>	map								   = new HashMap<>();
	protected MsgThumbImageView				thumbnailImageView;
	private List<MsgListItem>				messageItems					   = new ArrayList<>();
	private TextView						progressTextView;
	private ArrayList<String>				urlList							   = new ArrayList<>();
																			   
	@Override
	protected int getResId() {
		return R.layout.picture_message_view_right_item;
	}
	
	@Override
	protected int getViewHolderDirection() {
		return VIEW_HOLDER_RIGHT;
	}
	
	public void inflate() {
		super.inflate();
		
		thumbnailImageView = (MsgThumbImageView) view.findViewById(R.id.imageViewThumbnail);
		if (Build.VERSION.SDK_INT >= MIN_SDK_ENABLE_LAYER_TYPE_HARDWARE)
		{
			thumbnailImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
		
		longClickView = thumbnailImageView;
		progressLayout = view.findViewById(R.id.progressLayout);
		progressTextView = (TextView) view.findViewById(R.id.progressTextView);
//		messageItems.addAll(AbsBaseMessageActivity.items);
//		map.clear();
//		messageItems2.clear();
//		for (int i = 0; i < messageItems.size(); i++)
//		{
//			if (messageItems.get(i).getMessage().getAttachment() != null && (messageItems.get(i).getViewHolderType() == MsgViewHolderType.PICTURE_RIGHT || messageItems.get(i).getViewHolderType() == MsgViewHolderType.PICTURE_LEFT))
//			{
//				//				String url = ((ImageAttachment) messageItems.get(i).getMessage().getAttachment()).getPath();
//				//				urlList.add(url);
//				messageItems2.add(messageItems.get(i));
//				map.put(i, messageItems2.size() - 1);
//			}
//
//		}
	}
	
	public void refresh(Object item) {
		super.refresh(item);
		setLayoutParams(getImageDefWidth(), getImageDefHeight(), thumbnailImageView);
		
		ImageAttachment msgAttachment = (ImageAttachment) messageItem.getMessage().getAttachment();
		String imagePath = msgAttachment.getPath();
		String thumbPath = msgAttachment.getThumbPath();
		if (!TextUtils.isEmpty(thumbPath))
		{
			loadThumbnailImage(thumbPath);
		}
		else if (!TextUtils.isEmpty(imagePath))
		{
			loadThumbnailImage(imagePath);
		}
		else if (messageItem.getMessage().getAttachStatus() == AttachStatusEnum.transferred)
		{
			redownload();
		}
		
		thumbnailImageView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				ArrayList<MsgListItem> msgListItems = new ArrayList<>();
				int imgPosition = 0;
				for (int i = 0; i < items.size(); i++) {
					IMMessage imMessage = items.get(i).getMessage();
					if (imMessage.getAttachment() != null && imMessage.getMsgType() == MsgTypeEnum.image) {
						msgListItems.add(items.get(i));
						if (position == i) {
							imgPosition = msgListItems.size()-1;
						}
					}
				}
				WatchMessagePictureActivity.start(context, msgListItems, imgPosition);
//				WatchMessagePictureActivity.start(context, (ArrayList<MsgListItem>) messageItems2, position);
				//				Intent atlasSeeIntent = new Intent(context, NewInteractionAtlasSeeActivity.class);
				//				atlasSeeIntent.putExtra(DefaultConsts.NI_ATLASSEE_URL, urlList);
				//				atlasSeeIntent.putExtra(DefaultConsts.FROM_TYPE, DefaultConsts.NI_FROM_WEBURL);
				//				atlasSeeIntent.putExtra(DefaultConsts.NI_POSITION, map.get(position));
				//				context.startActivity(atlasSeeIntent);
			}
		});
		
		progressTextView.setText(StringUtil.getPercentString(((MsgListItem) item).progress));
	}
	
	private void loadThumbnailImage(String thumbPath) {
		ImageSize imageSize = ImageUtil.getThumbnailDisplaySize(getImageMaxEdge(), getImageMinEdge(), thumbPath);
		int width = imageSize.width;
		int height = imageSize.height;
		
		setLayoutParams(width, height, thumbnailImageView);
		Bitmap bitmap = thumbnailImageView.loadAsPath(thumbPath, width, height, R.drawable.message_right_blue_bg, R.drawable.message_right_blue_cover_bg);
		thumbnailImageView.setImageBitmap(bitmap);
	}
}
